from Model import Actor
from Model import Model
from Model import Distributions
from Model import Evidence
from segmentation.Formatter import Formatter
from segmentation.ChangepointFinder import ChangepointFinder
from segmentation.Move import MoveSegment
from models.Walk import Walk
from models.Loiter import Loiter
from models.Run import Run
from models.Standstill import Standstill
import sys
import numpy as np
import xml.etree.ElementTree as elet
    
if __name__ == "__main__":
    actors_to_evidence = dict()
    actors_to_changepoint_finders = dict()
    names_to_actors = dict()

    my_formatter = Formatter()

    actor_list = my_formatter.process_pol_tracks_from_file("track_64.txt")
    # actor_list = my_formatter.process_pol_tracks_from_file("pol.txt")
    actor_list = sorted(actor_list)

    i = 0
    for a in actor_list:
        names_to_actors[a.name] = a
        kw_dict = dict()
        arg_stat_funcs = [MoveSegment.identity_stat]
        arg_predictive_func = MoveSegment.expon_predictive
        arg_chi_prior = [.5]
        arg_nu_prior = 1.
        arg_actor = a
        arg_n_frames_for_params = 2
        arg_hazard_rate = .02
        arg_false_chi = [1./.15]
        arg_true_chi = [1./.8]
        arg_false_nu = 120.
        arg_true_nu = 60.
        arg_model_func = MoveSegment.expon_joint
        arg_model_params = [
            [Evidence.MOVE_FAST, [1./1.2]], [Evidence.MOVE_SLOW, [1./.5]],
            [Evidence.NOT_MOVE, [1./.05]]]
        actors_to_changepoint_finders[a] = ChangepointFinder(
            param_func=MoveSegment.calculate_speed, stat_funcs=arg_stat_funcs,
            predictive_func=arg_predictive_func, chi_prior=arg_chi_prior,
            nu_prior=arg_nu_prior, actor=arg_actor,
            n_frames_for_params=arg_n_frames_for_params,
            hazard_rate=arg_hazard_rate, false_chi=arg_false_chi,
            false_nu=arg_false_nu, true_chi=arg_true_chi, true_nu=arg_true_nu,
            model_func=arg_model_func, model_params=arg_model_params)
        evidence_list = my_formatter.make_evidence_single(a)
        actors_to_evidence[a] = evidence_list
        test_actor = a
        i += 1
        if i > 0:
            break

    aggregate_evidence_list = []
    for n, a in names_to_actors.iteritems():
        print "test actor has properties %s" % a
        print "test actor has width %s" % a.width
        evidence_list = actors_to_evidence[a]
        changepoint_finder = actors_to_changepoint_finders[a]
        n_frames = 0
        for e in evidence_list:
            n_frames += len(e.locations)
            print "adding evidence with properties %s" % e
            changepoint_finder.add_segment(e)
        print "added %s total frames" % n_frames
        xml = changepoint_finder.generate_changepoint_xml()
        xml_tree = elet.ElementTree(xml)
        xml_root = xml_tree.getroot()
        xml_list = xml_root.findall("interval")
        segmented_evidence_list = a.make_evidence_from_xml(xml_list)
        aggregate_evidence_list.extend(segmented_evidence_list)

    test_model = Loiter()
    for e in aggregate_evidence_list:
        print e
        test_model.model.add_evidence(e)

    test_model.model.evaluate_evidence()
    test_model.model.print_xml()


# def expon_predictive_joint(chi_prior, nu_prior, chi_data, nu_data):
#     chi_prior = chi_prior[0]
#     chi_data = chi_data[0]
#     # def log_factorial_ratio(begin,end):
#     #     begin = np.int(begin)
#     #     end = np.int(end)
#     #     cumulative_sum = 0.
#     #     for i in range(end-begin):
#     #         cumulative_sum += np.log(np.float(begin+i+1))
#     #     return cumulative_sum
#     def log_factorial(x):
#         x = np.int(x)
#         cumulative_sum = 0.
#         for i in range(x):
#             cumulative_sum += np.log(i+1)
#         return cumulative_sum
#     p_string = ("in predictive joint with chi_prior = %s,"
#                 " chi_data = %s, and nu_data = %s")
#     print (p_string % (chi_prior, chi_data, nu_data+1))
#     return ((nu_prior+1)*np.log(chi_prior) + log_factorial(nu_data) -
#             (nu_data+1)*np.log(chi_data) - log_factorial(nu_prior))
